<template>
  <div class="category">
    <div class="category-opera">
      <van-button type="default" size="mini" @click="useOperatorBoxStore().showOverlay(false, '')">取消</van-button>
      <span>筛选类别</span>
      <van-button type="primary" size="mini" @click="handleConfirm">完成</van-button>
    </div>
    <div class="category-options">
      <div class="category-options-items">
        <template v-for="(item, index) in options" :key="index">
          <div class="category-options-item" :class="{'select':item.checked.value}" @click="handleClick(index)">
            <span>{{ item.title }}</span>
            <i
                class="iconfont"
                :class="item.icon"
                :style="{ color: item.color }"
            ></i>
          </div>
        </template>
      </div>
    </div>
    <div class="category-more">
      <div class="category-more-title">有0个已归档卡片</div>
      <div class="category-more-content">
        <span class="category-options-item">切换查看已归档内容</span>
        <van-switch
            v-model="checked"
            size="20"
            active-color="#38c537"
            inactive-color="#d9d9db"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
// 作者：YCH
// 时间：2022年10月31日 22:31:06
import {ref, toRaw, watchEffect} from "vue";
import useArticleFilterStore from "@/stores/modules/articleFilter.js";
import useOperatorBoxStore from "@/stores/modules/operatorBox.js";
import {checkAndPush, removeIfExist} from "@/utils/index.js";
const checked = ref(false);
const emit = defineEmits(["handleCategoryConfirm"]);
const options = [
  {title: "不限类别", icon: "", color: "", checked: ref(true)},
  {title: "文章", icon: "icon-wenzhang2", color: "#070807", checked: ref(false)},
  {title: "片段", icon: "icon-ic_jian", color: "#070807", checked: ref(false)},
  {title: "网页", icon: "icon-Web", color: "#3c76f7", checked: ref(false)},
  {title: "速记", icon: "icon-lianxi2hebing_jilu", color: "#f97a02", checked: ref(false)},
  {title: "图片", icon: "icon-fl-tupian", color: "#f855a4", checked: ref(false)},
  {title: "音频", icon: "icon-yinpinpianduan", color: "#38c536", checked: ref(false)},
  {title: "视频", icon: "icon-shipin", color: "#239722", checked: ref(false)},
  {title: "文件", icon: "icon-wenjian", color: "#7950d5", checked: ref(false)}
];
/** 监听options,当除options[0]以外的checked值为true时，options[0].checked为false */
watchEffect(() => {
  for (let i = 1; i < options.length; i++) {
    if (options[i].checked.value) {
      options[0].checked.value = false;
      break;
    }
  }
});

/** 点击事件 */
const handleConfirm = () => {
  emit("handleCategoryConfirm");
}

/** 点击事件 */
const handleClick = (index) => {
  if (useArticleFilterStore().queryParams.collectTypeIds != undefined){
    if(options[index].checked.value){
      // 如果该checked为true（点击后变false），则移除该元素
      useArticleFilterStore().queryParams.collectTypeIds = removeIfExist(useArticleFilterStore().queryParams.collectTypeIds,getType(options[index].title))
    }else {
      // 如果该checked为false（点击后变true），则添加该元素
      checkAndPush(useArticleFilterStore().queryParams.collectTypeIds,getType(options[index].title))
    }
    if (useArticleFilterStore().queryParams.collectTypeIds.length > 0){
      useArticleFilterStore().queryParams.collectTypeIds.sort()
    }
  }else {
    useArticleFilterStore().queryParams.collectTypeIds = []
    useArticleFilterStore().queryParams.collectTypeIds.push(getType(options[index].title))
  }
  options[index].checked.value = !options[index].checked.value
}
/** 获取collectType */
const getType = (val) => {
  if (val == '图片') {
    return 100;
  } else if (val == '视频') {
    return 101;
  } else if (val == '音频') {
    return 102;
  } else if (val == '片段') {
    return 103;
  } else if (val == '速记') {
    return 104;
  } else if (val == '文件') {
    return 105;
  } else if (val == '文章') {
    return 106;
  } else if (val == '网页') {
    return 107;
  } else {
    return null;
  }
}
</script>

<style lang="less" scoped>
.category {
  padding: 10px 12px;
  font-family: var(--primary-font-family);
  font-weight: bold;
  color: var(--primary-font-color);

  .category-opera {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .category-options {
    position: relative;
    top: 6px;

    .category-options-items {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;

      .category-options-item {
        margin: 4px;
        padding: 0 6px;
        width: 43%;
        height: 38px;
        background-color: var(--primary-background-color);
        border-radius: 6px;
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
      }

      .category-options-item:nth-child(1) {
        width: 100%;
        justify-content: center;
      }

      .select {
        background-color: var(--primary-select-background-color);
        color: var(--primary-select-font-color);
      }
    }
  }

  .category-more {
    .category-more-title {
      height: 24px;
      line-height: 24px;
      margin: 4px;
      font-size: 12px;
      font-weight: 100;
    }

    .category-more-content {
      display: flex;
      justify-content: space-between;
      align-items: center;
      background-color: var(--primary-background-color);
      border-radius: 6px;
      height: 38px;
      padding: 0 6px;
      margin: 4px;
    }
  }
}
</style>
